/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.uct.common.entity.User;
import org.apache.log4j.Logger;

/**
 * description:  根据单个用户属性查询对象，一般为userId,userEname(email,mobile)这种唯一属性的
 *
 * @author iussoft 创建时间：2016-3-26
 */
public class UserEntityPlugin implements PluginConnector {

    private Logger log = Logger.getLogger(UserEntityPlugin.class);

    private IDao userDao;

    /**
     * TODO 根据单个用户属性查询对象，一般为userId,userEname(email,mobile)这种唯一属性的  <br>
     * 参数说明:<br>
     * userEname:用户账号;<br>
     * userId:用户ID;<br>
     * email:邮箱;<br>
     * mobile:手机
     */
    public Object execute(ParamsVo vo) throws Exception {
        ResultEntity result = new ResultEntity();
        try {
            result.setResult("true");
            MethodParam param = new MethodParam("ByUserAttr", "", "", User.class.getName());
            StringBuffer sb = new StringBuffer();
            if (Validator.isNotNull(String.valueOf(vo.getParams("userEname")))) {
                sb.append(" and userEname =:userEname");
                param.setParams("userEname", String.valueOf(vo.getParams("userEname")));
            }
            if (Validator.isNotNull(String.valueOf(vo.getParams("userId")))) {
                sb.append(" and userId =:userId");
                param.setParams("userId", String.valueOf(vo.getParams("userId")));
            }
            if (Validator.isNotNull(String.valueOf(vo.getParams("email")))) {
                sb.append(" and email =:email");
                param.setParams("email", String.valueOf(vo.getParams("email")));
            }
            if (Validator.isNotNull(String.valueOf(vo.getParams("mobile")))) {
                sb.append(" and mobile =:mobile");
                param.setParams("mobile", String.valueOf(vo.getParams("mobile")));
            }
            if (!Validator.isNull(sb.toString())) {
                param.setSqlStr(userDao.getSql(2) + sb.toString() + " and userStatus > -1");
                User user = (User) userDao.selectSingle(param);
                result.setEntity(user);
            } else {
                result.setEntity("方法参数为空或用户不存在");
            }
        } catch (Exception e) {
            result.setResult("false");
            log.error("执行机构查询失败 ", e);
            result.setMessage("机构查询失败！");
        }
        return result;
    }

    public void setUserDao(IDao userDao) {
        this.userDao = userDao;
    }

}
